using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace UnityGameFramework.Editor
{
    public sealed class GameFrameworkSettings : ScriptableObject
    {
        [Header("Resources Editor")]
        public string UnionTypeFilter = "t:Scene t:Prefab t:Shader t:Model t:Material t:Texture t:AudioClip t:AnimationClip t:AnimatorController t:Font t:TextAsset t:ScriptableObject";
        public string UnionLabelFilter = "l:ResourceInclusive";
        public string ExceptTypeFilter = "t:Script";
        public string ExceptLabelFilter = "l:ResourceExclusive";

        [Header("Types Searched Scope Definitions")]
        public List<string> EditorSearchedScopeAssemblyNames = new List<string>();
        public List<AssemblyDefinitionAsset> EditorSearchedScopeDefinitions = new List<AssemblyDefinitionAsset>();
        
        public List<string> RuntimeSearchedScopeAssemblyNames = new List<string>();
        public List<AssemblyDefinitionAsset> RuntimeSearchedScopeDefinitions = new List<AssemblyDefinitionAsset>();

        public static readonly string s_DefaultName = "GameFrameworkSettings";

        private static GameFrameworkSettings s_Instance = null;

        public static GameFrameworkSettings SettingObject
        {
            get
            {
                if (s_Instance != null) return s_Instance;
                string path = $"ProjectSettings/{GameFrameworkSettings.s_DefaultName}.asset";
                if (File.Exists(path))
                {
                    s_Instance = (GameFrameworkSettings)(InternalEditorUtility.LoadSerializedFileAndForget(path).Single());
                }
                else
                {
                    SettingObject = ScriptableObject.CreateInstance<GameFrameworkSettings>();
                }
                return s_Instance;
            }

            set
            {
                s_Instance = value;
                string path = $"ProjectSettings/{GameFrameworkSettings.s_DefaultName}.asset";
                InternalEditorUtility.SaveToSerializedFileAndForget(new[] { value }, path, true);
            }
        }

        public static void Save()
        {
            string path = $"ProjectSettings/{GameFrameworkSettings.s_DefaultName}.asset";
            InternalEditorUtility.SaveToSerializedFileAndForget(new[] { SettingObject }, path, true);
        }
    }
}
